home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 399 < prev    next >
Internet Message Format  |  1996-08-06  |  3KB

  1. Path: solon.com!not-for-mail
  2. From: ok@cs.rmit.edu.au (Richard A. O'Keefe)
  3. Newsgroups: comp.std.c,comp.lang.c.moderated
  4. Subject: Re: Integral promotion.
  5. Date: 15 Feb 1996 08:49:50 -0600
  6. Organization: Comp Sci, RMIT, Melbourne, Australia
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4fvh6f$e29@solutions.solon.com>
  10. References: <4fstj7$2l6@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12. X-Newsreader: NN version 6.5.0 #0 (NOV)
  13.  
  14. Rune Huseby <rune.huseby@gpi.telemax.no> writes:
  15. >short test(short x1, short x2)
  16. >{
  17. >  short result;
  18. >  result = x1 + x2;  /* Warning:  '=' : conversion from 'int '                                 
  19. >                         to 'short ', possible loss of data */
  20. >  return result;
  21. >}
  22.  
  23. Your compiler is right.  "Integral promotion" means that before anything
  24. else happens to them in an arithmetic expression, signed char and short
  25. promote to int, and unsigned char and unsigned short promote to unsigned int.
  26. Your compiler has to be able to implement arithmetic on
  27.     signed int        signed long
  28.     unsigned int        unsigned long
  29. but not on any other integral types.  Note in particular that in a system
  30. where INT_MAX > SHRT_MAX loss of data really _is_ possible:
  31.  
  32.     short x1 = SHRT_MAX;
  33.     short x2 = SHRT_MAX;
  34.     short result = x1 + x2;    /* OVERFLOW! */
  35.  
  36. On a 16-bit sloppy system, the chances are pretty good that you'd get
  37.     result < 0
  38. but on a 32-bit system, sloppy or not, this example must yield
  39.     result > 0.
  40. And remember, on a MIPS, Alpha, SPARC, 88k, &c there _is_ no 16-bit add
  41. instruction; it _has_ to be done in 32-bit mode.
  42.  
  43. Yes, there are C compilers that generate code that raises an exception if
  44. a signed integer overflow occurs.  I've used one.  It's *extremely* useful
  45. because it's almost always a mistake.  If you really want wraparound, you
  46. can use unsigned arithmetic.
  47.  
  48. >The reason I ask is that I am writing code that should be 
  49. >portable from a 16-bits environment (where short and int are the 
  50. >same size) to a 32-bits environment. (My 16-bits compiler does 
  51. >not complain about the code).
  52.  
  53. Well, there's no way to get exactly the same set of warnings on every
  54. system; a compiler can legitimately complain about the colour of your
  55. eyes if it wants to, as long as it correctly translates the code.
  56.  
  57. It is really really worth investing in a 'lint' checker.  If you're using
  58. a DOS system, I've heard good things about Gimpel's lint.
  59. -- 
  60. Election time; but how to get Labour _out_ without letting Liberal _in_?
  61. Richard A. O'Keefe; http://www.cs.rmit.edu.au/~ok; RMIT Comp.Sci.
  62.